﻿/********************************************************************************
** 作者： 易景安
** 创建时间： 2011-11-15
** 描述：
** 部门管理-涉及到的相关方法
********************************************************************************/

var sectionTree         // 部门树
var OrganID = '';       // 部门ID
var parentID = '';       // 父节点ID
var isEdit = false;
var IsLeaf = false;
var LevelNo;
var storeUser;     // 用户数据源
var gridUser;      // 用户Grid
var pageSize = 20; // 每页显示数
var winAddUser;    // 打开用户窗体
var winEditUser;   // 打开用户窗体
var UserID;
var isSameLevel = false    // 是否同级菜单
var ParentOrganID = '';      // 父节点ID
var OrganUserForm; var OrganUserWindow;
var OrganRoleForm; var OrganRoleWindow;
var fromStoreUser; var toStoreUser;
var OrganName;  //部门名称
Ext.onReady(function() {
    Ext.QuickTips.init();

    // 本地加载图片代码
    Ext.BLANK_IMAGE_URL = strPath + 'extjs/resources/images/default/s.gif';

    // 新增部门窗体 #开始 ---------------------------------------------------------------------------------------------------------------------------
    // 新增部门信息面板底部按钮
    winOrganization = new Ext.Window({
        title: '新增部门',
        width: 420,
        height: 268,
        autoScroll: true,
        bodyStyle: 'border-top:0;padding:2 2 2 2;background:#E5E2EE;',
        closeAction: 'hide',
        plain: true,
        modal: true,
        bodyStyle: 'padding:5px;',
        buttonAlign: 'center',
        contentEl: 'divOrganization',
        buttons: [{
            text: '确定',
            id: 'btnSerachInfo',
            name: 'btnSerachInfo',
            handler: function() {
                AddOrganization();
            }
        }, {
            text: '关闭',
            handler: function() {
                winOrganization.hide();
            }
        }
        ]
    });
    // 部门树对象的生成 #开始 ---------------------------------------------------------------------------------------------------------------------------
    // 部门树根节点
    var rootNodeTrade = { nodeType: 'async', text: '部门列表', draggable: false, id: 'OrganizationTree', collapsed: false, expanded: true };
    sectionTree = new Ext.ux.tree.TreePanel({
        renderTo: "divOrganizationTree",
        animate: true,
        enableDD: true,
        enableDrag: true,
        enableDrop: true,
        rootVisible: true,
        autoScroll: true,
        useArrows: false,
        border: false,
        height: parent.Ext.get("ContentFrame").getHeight() - 25,
        lines: true,
        root: rootNodeTrade,
        loader: new Ext.ux.tree.TreeLoader(
            {
                dataUrl: 'GetOrganizationTree.aspx'
            }),
        listeners: {
            "contextmenu": function(node, e) {
                if (node.id != null) {
                    OrganID = node.id;
                    IsLeaf = node.isLeaf();
                    LevelNo = node.getDepth() + 1;
                    if (node.parentNode == null) ParentOrganID = 0;
                    else if (node.parentNode.id == 'OrganizationTree') ParentOrganID = 0;
                    else ParentOrganID = node.parentNode.id;
                    var nodemenu = new Ext.menu.Menu({
                        items: [{
                            text: "新增子部门",
                            iconCls: 'addOrganization',
                            handler: function() {
                                CleanOrgan();
                                isSameLevel = false;
                                winOrganization.setTitle('&nbsp;&nbsp;新增部门');
                                isEdit = false;
                                setValue('txtParentName', node.text);
                                controlShow('divOrganization');
                                winOrganization.show();
                            }
                        }, {
                            text: "新增同级部门",
                            id: 'AddSameOrganization',
                            iconCls: 'folder',
                            handler: function() {
                                CleanOrgan();
                                isSameLevel = true;
                                winOrganization.setTitle('&nbsp;&nbsp;新增部门');
                                isEdit = false;
                                setValue('txtParentName', node.parentNode.text);
                                controlShow('divOrganization');
                                winOrganization.show();
                            }
                        }, {
                            text: "编辑部门",
                            iconCls: 'folder',
                            id: 'editOrganization',
                            handler: function() {
                                isEdit = true;
                                winOrganization.setTitle('&nbsp;&nbsp;编辑部门');
                                setValue('txtParentName', node.parentNode.text);
                                GetOrganInfo(node.id);
                                controlShow('divOrganization');
                                winOrganization.show();
                            }
                        }, {
                            text: "删除部门",
                            id: 'delOrganization',
                            iconCls: 'folder',
                            handler: function() {
                                Ext.MessageBox.confirm('提示', '确实要删除选中的部门吗？该部门及部门下的子部门也将被删除！', DeleteOrganization);
                            }
                        }
                        ]
                    });
                    nodemenu.showAt(e.getPoint());
                    if (node.id == "OrganizationTree") {
                        nodemenu.items.get('AddSameOrganization').setDisabled(true);
                        // nodemenu.items.get('editOrganUser').setDisabled(true);
                        nodemenu.items.get('editOrganRole').setDisabled(true);
                        nodemenu.items.get('editOrganization').setDisabled(true);
                        nodemenu.items.get('delOrganization').setDisabled(true);
                    }
                }
            }
        }
    });
    // Tree的点击事件。
    sectionTree.on('click', function(node) {
        if (node.id != "OrganizationTree" && node.id != "") {
            OrganID = node.id;
            OrganName = node.text;
            doSearchUser(true);
        }
    });
    // Tree的点击事件。
    sectionTree.on('dblclick', function(node) {
        if (node.id != "OrganizationTree" && node.id != "") {
            isEdit = true;
            winOrganization.setTitle('&nbsp;&nbsp;编辑部门');
            setValue('txtParentName', node.parentNode.text);
            GetOrganInfo(node.id);
            controlShow('divOrganization');
            winOrganization.show();
        }
    });
    sectionTree.on('expandnode', function(node) {
        if (!node.isSelected() && sectionTree.root.firstChild != null)
            sectionTree.selectNodeById(sectionTree.root.firstChild.id);
    });
    // 加载部门Tree
    loadTree();


    // 选择人员列表
    var httpProxy = new Ext.data.HttpProxy({
        url: strPath + 'GGFW/BaseInfo/FoothillUserCmd.fwf?Method=GetFoothillUserByPage'
    });
    // 人员的读取方式
    var readerProxy = new Ext.data.JsonReader({
        root: 'Records',
        totalProperty: 'totalCount',
        fields: [
                  { name: 'UserID' },
                  { name: 'UserName' },
                  { name: 'TrueName' },
                  { name: 'OrganID' },
                  { name: 'Sex' },
                  { name: 'Status' },
                  { name: 'MobilePhone' },
                  { name: 'Email' },
                  { name: 'Remark' },
                  { name: 'OrganName' },
                  { name: 'Position' },
                  { name: 'OrderID' }
             ]
    });
    // 定义人员数据源
    storeUser = new Ext.data.Store({
        proxy: httpProxy,
        reader: readerProxy,
        remoteSort: true
    });
    // 设置显示列的Title
    var ChooseView = new Ext.grid.GridView({ forceFit: true, sortAscText: '升序', sortDescText: '降序' });
    ChooseView.columnsText = '列显示/隐藏';
    // 加载前的事件
    storeUser.on('beforeload', function() {
        var UserName = Ext.get("txtUserName").dom.value;
        var Status = Ext.get("chStatus").dom.checked;
        var Forbid = Ext.get("chStatusNo").dom.checked;
        if (Status && Forbid) Status = 2;
        else if (Status && !Forbid) Status = 1;
        else if (!Status && Forbid) Status = 0;
        else Status = "3";
        var TrueName = Ext.get("txtTrueName").dom.value;
        storeUser.baseParams = {
            UserName: UserName, TrueName: TrueName, Status: Status
        };
    });
    // 设置页脚的显示样式。
    var ChooseBar = new Ext.PagingToolbar({
        pageSize: pageSize,
        store: storeUser,
        beforePageText: '第',
        afterPageText: '页 共{0}页',
        firstText: '第一页',
        prevText: '前一页',
        nextText: '后一页',
        lastText: '最后一页',
        refreshText: '刷新',
        displayInfo: true,
        displayMsg: '显示第 {0} - {1} 条数据, 共 {2} 条数据',
        emptyMsg: "没有数据可以显示"
    });
    // grid的选择列
    var smUserListModel = new Ext.grid.CheckboxSelectionModel({
        singleSelect: false,
        listeners: {
            rowselect: function(sm, row, rec) {
            }
        }
    });
    var GridToolBar = new Ext.Panel({
        labelAlign: 'top',
        frame: false,
        bodyStyle: 'border:0px;',
        width: "100%",
        html: '<table width="100%" border="0" cellspacing="0" cellpadding="0" style="background:#BFEAFD;"><tr><td width="70" align="right" height="26">登录账号：</td><td align="left" style="width:420px;"><table border="0" cellspacing="0" cellpadding="0"><tr><td width="79" align="left"><input type="text" id="txtUserName"  style="width:90px;"/></td><td width="70" align="center">&nbsp;&nbsp;用户姓名：</td><td align="left"><input type="text" id="txtTrueName" style="width:90px;"/></td><td width="50" align="right">状态：</td><td><input name="chStatus" type="checkbox" value="1" checked="true" />启用<input name="chStatusNo" type="checkbox" value="0" checked="true" />禁用</td></tr></table></td><td align="left"><input name="button" type="button" style="padding: 0px 5px; height: 22px;" id="btnSearchAll" onclick="doSearchUser(true);" value="查 询" /></td></tr><tr id="RoleManage"><td height="26" colspan="5" align="left"><table border="0" cellspacing="0" cellpadding="0"><tr><td width="28" height="20" align="center" id="divAddRoleInfoImg"><img src="images/ico_28.gif" width="16" height="16" /></td><td width="40" class="general_h1_gray_c01_font" id="divAddRoleInfo"><span style="cursor: hand;text-decoration:underline;" onclick="ShowUserWin();">新增</span></td><td width="24" align="center" class="general_h1_gray_c01_font" id="divAcceptImg"><img src="images/divwin_ico_6.gif" width="16" height="16" /></td><td width="65" align="left" class="general_h1_gray_c01_font" id="divAccept"><span style="cursor: hand; text-decoration: underline;" onclick="ShowSetUserPassWin();">修改密码</span></td><td width="24" align="center" class="general_h1_gray_c01_font" id="divAllotImg"><img src="images/ico_23.gif" width="16" height="16" /></td><td width="40" align="left" class="general_h1_gray_c01_font" id="divAllot"><span style="cursor: hand; text-decoration: underline;" onclick="SetStartStatus();">启用</span></td><td width="24" align="center" class="general_h1_gray_c01_font" id="divFeedbackImg"><img src="../images/ico_25.gif" width="16" height="16" /></td><td width="40" align="left" class="general_h1_gray_c01_font" id="divFeedback"><span style="cursor: hand; text-decoration: underline;" onclick="SetStopStatus();">禁用</span></td><td width="28" style="display=none" align="center" class="general_h1_gray_c01_font" id="divCancellationImg"><img src="images/ico_21.gif" width="16" height="16" /></td><td width="40" style="display=none" align="left" class="general_h1_gray_c01_font" id="divCancellation"><span style="cursor: hand;text-decoration:underline;" onclick="DelUser();">删除</span></td></tr></table></td></tr></table>'
    });
    // 用户Grid的定义
    gridUser = new Ext.grid.GridPanel({
        id: 'pagedef-grid',
        store: storeUser,
        renderTo: "divUserInfo",
        cm: new Ext.grid.ColumnModel([
            smUserListModel,
            { id: 'UserID', header: "用户ID", sortable: false, dataIndex: 'UserID', hidden: true },  // ,renderer:renderOpenUserInfo
            {header: "登录账号", sortable: true, dataIndex: 'UserName' },
            { header: "用户姓名", sortable: true, dataIndex: 'TrueName', renderer: OpenUserInfo },
            { header: "所在部门", sortable: true, dataIndex: 'OrganName' },
            { header: "性别", sortable: true, dataIndex: 'Sex', renderer: SetSex },
            { header: "手机", sortable: true, dataIndex: 'MobilePhone' },
            { header: "邮箱", sortable: true, dataIndex: 'Email' },
            { header: "状态", sortable: true, dataIndex: 'Status', renderer: SetStatus },
            { header: "备注", sortable: false, dataIndex: 'Remark' }
        ]),
        sm: smUserListModel,
        height: parent.Ext.get("ContentFrame").getHeight() - 25,
        bbar: ChooseBar,
        view: ChooseView,
        frame: false,
        border: false,
        loadMask: { msg: '正在加载数据，请稍侯... ...' },
        tbar: GridToolBar
    });

    gridUser.addListener('rowdblclick', function(gridObj, rowIdx, e) {
        // GetChoosePoint();
    });

    // 查看用户
    function OpenUserInfo(value, metadate, record, colIndex, rowIndex) {
        return "<a href=\"javascript:GetUserInfo('" + record.get("UserID") + "')\" title='" + value + "'>" + value + "</a>";
    }
    // 设置用户性别
    function SetSex(value, metadate, record, colIndex, rowIndex) {
        if (value == '1') return "男";
        else if (value == '0') return "女";
        else return "男";
    }
    // 设置用户状态
    function SetStatus(value, metadate, record, colIndex, rowIndex) {
        if (value == 1) return "启用";
        else return "禁用";
    }
    // 用户查询
    doSearchUser(true);
});


var GridToolBar = new Ext.Panel({
    labelAlign: 'top',
    frame: true,
    //bodyStyle: 'border-bottom:0;',// padding:2 2 2 2;background:#E5E2EE;
    width: "100%",
    html: '<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td width="66" align="right" height="26">用户姓名：</td><td align="left" style="width:265px;"><table border="0" cellspacing="0" cellpadding="0"><tr><td width="79" align="left"><input type="text" class="input_l95p_style" style="width: 150;" id="txtChTrueName"></td><td><input name="chStatus" type="radio" value="1" checked="true" />其他<input name="chStatus" type="radio" value="0" />本部门</td></tr></table></td><td align="left"><input name="button" type="button" class="btn_general_style" id="btnSearchAll" onclick="LoadSearch();" value="查 询" /></td></tr></table>'
});

//创建一个HttpProxy
var fromHttpProxy = new Ext.data.HttpProxy({
    url: strPath + 'GGFW/BaseInfo/FoothillOrganizationCmd.fwf?Method=SearchNotOrganUsers'
});

//创建一个JsonReader
var fromRecord = new Ext.data.JsonReader({
    root: 'data',
    fields: [
                  { name: 'UserID' },               //人员编码
                  {name: 'TrueName'}              //人员名称
               ]
});
// 定义选择人员数据源
fromStoreUser = new Ext.data.Store({
    proxy: fromHttpProxy,
    reader: fromRecord,
    remoteSort: true
});
//创建一个HttpProxy,已选择人员
var toHttpProxy = new Ext.data.HttpProxy({
    url: strPath + 'GGFW/BaseInfo/FoothillOrganizationCmd.fwf?Method=SearchOrganUsers'
});
//创建一个JsonReader，已选择人员
var toRecord = new Ext.data.JsonReader({
    root: 'data',
    fields: [
                  { name: 'UserID' },               //人员编码
                  {name: 'TrueName'}              //人员名称
               ]
});
// 定义已选择人员数据源
toStoreUser = new Ext.data.Store({
    proxy: toHttpProxy,
    reader: toRecord,
    remoteSort: true
});

//定义一个Form,角色菜单分配
OrganUserForm = new Ext.form.FormPanel({
    width: 480,
    border: false,
    layout: 'fit',
    bodyStyle: 'padding:4px;',
    items: [{
        xtype: "itemselector",
        name: "itemselector",
        dataFields: ["UserID", "TrueName"],
        valueField: "UserID",
        displayField: "TrueName",
        imagePath: strPath + "extjs/multiselect/images/",
        toLegend: "本部门人员",
        fromLegend: "其他部门人员",
        toStore: toStoreUser,
        fromStore: fromStoreUser
    }
    ],
    tbar: GridToolBar,
    buttons: [{
        text: '确定',
        handler: function() {
            if (OrganUserForm.getForm().isValid()) {
                UpdateOrganUsers(OrganID);
            }
        }
    },
    {
        text: '关闭',
        handler: function() {
            if (OrganUserForm.getForm().isValid()) {
                OrganUserWindow.hide();
            }
        }
    }
    ]
});

//定义一个窗体,部门对应人员权限分配
OrganUserWindow = new Ext.Window({
    title: '&nbsp;&nbsp;部门对应人员权限分配',
    width: 466,
    height: 438,
    layout: 'fit',
    plain: true,
    bodyStyle: 'padding:10px;',
    buttonAlign: 'center',
    items: OrganUserForm
});
// 设置部门人员
function SetOrganUser(OrganID) {
    OrganUserWindow.show();
    setValue('txtChTrueName', '');
    fromStoreUser.load({ params: { OrganID: OrganID} });
    toStoreUser.load({ params: { OrganID: OrganID} });
}

// 查询事件
function LoadSearch() {
    var Status = '';
    var obj = document.getElementsByName("chStatus");
    for (i = 0; i < obj.length; i++) {
        if (obj[i].checked) {
            Status = obj[i].value;
        }
    }
    var TrueName = Ext.get("txtChTrueName").dom.value;
    if (Status == '1') {
        fromStoreUser.load({ params: { OrganID: OrganID, TrueName: TrueName} });
        toStoreUser.load({ params: { OrganID: OrganID} });
    }
    else if (Status == '0') {
        toStoreUser.load({ params: { OrganID: OrganID, TrueName: TrueName} });
        fromStoreUser.load({ params: { OrganID: OrganID} });
    }
}
// 更新用户部门
function UpdateUserRole(OrganID, UserID) {
    // 获取选中的ID
    var strUrl = strPath + 'GGFW/BaseInfo/FoothillOrganizationCmd.fwf?Method=SetOrganUsers';
    Ext.Ajax.request({
        url: strUrl,
        method: 'POST',
        params: {
            OrganID: OrganID,
            UserIDs: UserID
        },
        failure: function(response, options) {
            ReturnValue = Ext.MessageBox.alert("警告", "网络异常或登录过期，请重新登录！");
        },
        success: function(response, options) {
            var json = response.responseText;
            var obj = parseJsonResult(json);

            if (!obj.Success) {
                showMessage("错误！", "设置菜单状态出现错误！请查看详细原因。", obj.Message);
            }
            else {
                // OrganUserWindow.hide();
                doSearchUser(true);
            }
        }
    });
}
// 打开选择用户的窗体
function OpenRoleOrganWin(OrganID) {
    // 提交
    var strUrl = strPath + 'GGFW/BaseInfo/FoothillOrganizationCmd.fwf?Method=SearchOrganRoles';
    Ext.Ajax.request({
        url: strUrl,
        method: 'POST',
        params: {
            OrganID: OrganID
        },
        failure: function(response, options) {
            ReturnValue = Ext.MessageBox.alert("警告", "网络异常或登录过期，请重新登录！");
        },
        success: function(response, options) {
            var json = response.responseText;
            var obj = parseJsonResult(json);

            if (obj == null || !obj.Success) {
                showMessage("错误！", "保存出现错误！请查看详细原因。", obj.Message);
            }
            else {
                var ids = "";
                for (var i = 0, len = obj.data.length; i < len; i++) {
                    if (i == 0)
                        ids = obj.data[i].RoleID + ',';
                    else
                        ids = ids + "," + obj.data[i].RoleID;
                }
                ShowRoleWin(ids, OrganID, '部门角色分配');
            }
        }
    });
}
// 打开选择用户的窗体
function OpenRoleUserWin(OrganID) {
    // 提交
    var strUrl = strPath + 'GGFW/BaseInfo/FoothillOrganizationCmd.fwf?Method=SearchOrganUsers'
    Ext.Ajax.request({
        url: strUrl,
        method: 'POST',
        params: {
            OrganID: OrganID
        },
        failure: function(response, options) {
            ReturnValue = Ext.MessageBox.alert("警告", "网络异常或登录过期，请重新登录！");
        },
        success: function(response, options) {
            var json = response.responseText;
            var obj = parseJsonResult(json);

            if (obj == null || !obj.Success) {
                showMessage("错误！", "保存出现错误！请查看详细原因。", obj.Message);
            }
            else {
                var ids = "";
                for (var i = 0, len = obj.data.length; i < len; i++) {
                    if (i == 0)
                        ids = obj.data[i].UserID + ',';
                    else
                        ids = ids + "," + obj.data[i].UserID;
                }
                ShowOrganUserWin(ids, OrganID, '部门用户分配');
            }
        }
    });
}
// 设置部门角色
function SetOrganRole(OrganID) {
    //创建一个HttpProxy
    var fromRoHttpProxy = new Ext.data.HttpProxy({
        url: strPath + 'GGFW/BaseInfo/FoothillOrganizationCmd.fwf?Method=SearchNotOrganRoles&OrganID=' + OrganID
    });

    //创建一个JsonReader
    var fromRoRecord = new Ext.data.JsonReader({
        root: 'data',
        fields: [
                      { name: 'RoleID' },               //人员编码
                      {name: 'RoleName'}              //人员名称
                   ]
    });
    // 定义选择人员数据源
    var fromRoStore = new Ext.data.Store({
        proxy: fromRoHttpProxy,
        reader: fromRoRecord,
        remoteSort: true
    });
    //创建一个HttpProxy,已选择人员
    var toRoHttpProxy = new Ext.data.HttpProxy({
        url: strPath + 'GGFW/BaseInfo/FoothillOrganizationCmd.fwf?Method=SearchOrganRoles&OrganID=' + OrganID
    });
    //创建一个JsonReader，已选择人员
    var toRoRecord = new Ext.data.JsonReader({
        root: 'data',
        fields: [
                      { name: 'RoleID' },               //人员编码
                      {name: 'RoleName'}              //人员名称
                   ]
    });
    // 定义已选择人员数据源
    var toRoStore = new Ext.data.Store({
        proxy: toRoHttpProxy,
        reader: toRoRecord,
        remoteSort: true
    });
    //定义一个Form,角色菜单分配
    OrganRoleForm = new Ext.form.FormPanel({
        width: 480,
        border: false,
        layout: 'fit',
        bodyStyle: 'padding:10px;',
        items: [{
            xtype: "itemselector",
            name: "itemselector",
            dataFields: ["RoleID", "RoleName"],
            valueField: "RoleID",
            displayField: "RoleName",
            imagePath: strPath + "extjs/multiselect/images/",
            toLegend: "本部门角色",
            fromLegend: "所有角色",
            toStore: toRoStore,
            fromStore: fromRoStore
}],
            buttons: [{
                text: '确定',
                handler: function() {
                    if (OrganRoleForm.getForm().isValid()) {
                        UpdateOrganRoles(OrganID);
                    }
                }
            },
            {
                text: '关闭',
                handler: function() {
                    if (OrganRoleForm.getForm().isValid()) {
                        OrganRoleWindow.hide();
                    }
                }
}]
            });

            //定义一个窗体,角色菜单分配
            OrganRoleWindow = new Ext.Window({
                title: '菜单对应角色权限分配',
                width: 480,
                height: 400,
                layout: 'fit',
                plain: true,
                bodyStyle: 'padding:10px;',
                buttonAlign: 'center',
                items: OrganRoleForm
            });
            OrganRoleWindow.show();
            fromRoStore.load();
            toRoStore.load();
        }
        // 更新部门角色
        function UpdateOrganRole(OrganID, RoleIDs) {
            // 获取选中的ID
            var strUrl = strPath + 'GGFW/BaseInfo/FoothillOrganizationCmd.fwf?Method=SetOrganRoles';
            Ext.Ajax.request({
                url: strUrl,
                method: 'POST',
                params: {
                    OrganID: OrganID,
                    RoleIDs: RoleIDs
                },
                failure: function(response, options) {
                    ReturnValue = Ext.MessageBox.alert("警告", "网络异常或登录过期，请重新登录！");
                },
                success: function(response, options) {
                    var json = response.responseText;
                    var obj = parseJsonResult(json);

                    if (!obj.Success) {
                        showMessage("错误！", "设置菜单状态出现错误！请查看详细原因。", obj.Message);
                    }
                }
            });
        }
        // 新增部门
        function AddOrganization() {
            var myMask = new Ext.LoadMask(Ext.getBody(), {
                msg: "正在加载数据..."
            });
            myMask.show();
            // 获取参数
            var OrganName = Ext.get("txtOrganName").dom.value;
            var OrganLabel = Ext.get("txtOrganLabel").dom.value;
            var status = Ext.get("OrganStatus").dom.checked;
            if (status) status = 1;
            else status = 0;
            var OrderID = Ext.get("txtOrderID").dom.value;
            var Remark = Ext.get("txtRemark").dom.value;
            if (IsLeaf) IsLeaf = 1;
            else IsLeaf = 0;
            if (!isSameLevel) ParentOrganID = OrganID;
            // 验证必填项
            var msg = "";
            var strLen = "";
            if (OrganName.trim() == "") msg += "部门名称 ";
            if (GetStrLength(OrganName) > 20) strLen += "部门名称 ";
            if (GetStrLength(OrganLabel) > 20) strLen += "部门描述 ";
            if (GetStrLength(Remark) > 50) strLen += "备注 ";
            if (msg != "" || strLen != "") {
                myMask.hide();
                Ext.MessageBox.alert("&nbsp;提示", (msg == "" ? "" : (msg + "不能为空！")) + (strLen == "" ? "" : ("<br/>" + strLen + "输入字符过长!")));
                return;
            }
            var strUrl = strPath + 'GGFW/BaseInfo/FoothillOrganizationCmd.fwf?Method=AddFoothillOrganizationData';
            if (isEdit) strUrl = strPath + 'GGFW/BaseInfo/FoothillOrganizationCmd.fwf?Method=UpdateFoothillOrganizationData';
            Ext.Ajax.request({
                url: strUrl,
                method: 'POST',
                params: {
                    ParentOrganID: ParentOrganID,
                    OrganID: OrganID,
                    IsLeaf: IsLeaf,
                    LevelNo: LevelNo,
                    OrganName: OrganName,
                    OrganLabel: OrganLabel,
                    status: status,
                    OrderID: OrderID,
                    Remark: Remark
                },
                failure: function(response, options) {
                    myMask.hide();
                    ReturnValue = Ext.MessageBox.alert("警告", "网络异常或登录过期，请重新登录！");
                },
                success: function(response, options) {
                    myMask.hide();
                    var json = response.responseText;
                    var obj = parseJsonResult(json);

                    if (!obj.Success) {
                        showMessage("错误！", "保存数据出错！", obj.Message);
                    }
                    else {
                        controlHide('divOrganization');
                        winOrganization.hide();
                        loadTree();
                    }
                }
            });
        }
        // 删除部门
        function DeleteOrganization(btn) {
            if (btn == "yes") {
                var strUrl = strPath + 'GGFW/BaseInfo/FoothillOrganizationCmd.fwf?Method=DeleteFoothillOrganization&OrganID=' + OrganID;
                Ext.Ajax.request({
                    url: strUrl,
                    method: 'POST',
                    failure: function(response, options) {
                        ReturnValue = Ext.MessageBox.alert("警告", "网络异常或登录过期，请重新登录！");
                    },
                    success: function(response, options) {
                        var json = response.responseText;
                        var obj = parseJsonResult(json);

                        if (!obj.Success) {
                            showMessage("错误！", "删除出现错误！请查看详细原因。", obj.Message);
                        }
                        else {
                            loadTree();
                        }
                    }
                });
            }
        }
        // 获取部门ID
        function GetOrganInfo(nodeID) {
            if (nodeID == null) return;
            var strUrls = strPath + 'GGFW/BaseInfo/FoothillOrganizationCmd.fwf?Method=GetFoothillOrganization&OrganizationID=' + nodeID;
            Ext.Ajax.request({
                url: strUrls,
                method: 'POST',
                failure: function(response, options) {
                    ReturnValue = Ext.MessageBox.alert("警告", "网络异常或登录过期，请重新登录！");
                },
                success: function(response, options) {
                    // 绑定值
                    var json = response.responseText;
                    var obj = parseJsonResult(json);

                    if (obj != null) {
                        if (!obj.Success) {
                            showMessage("错误！", "读取数据出错！", obj.Message);
                        }
                        else if (obj.data[0] != null) {
                            Ext.get("txtOrganName").dom.value = SiftNullAndEmpty(obj.data[0].OrganName);
                            Ext.get("txtOrganLabel").dom.value = SiftNullAndEmpty(obj.data[0].OrganLabel);
                            Ext.get("txtOrderID").dom.value = SiftNullAndEmpty(obj.data[0].OrderID);
                            Ext.get("txtRemark").dom.value = SiftNullAndEmpty(obj.data[0].Remark);
                            // 设置状态
                            var Status = SiftNullAndEmpty(obj.data[0].status);
                            if (Status == '1') Ext.get("OrganStatus").dom.checked = true;
                            else Ext.get("OrganStatus").dom.checked = false;
                        }
                    }
                    else {
                        showMessage("错误！", "读取数据出错！");
                    }
                }
            });
        }
        // 清除部门绑定信息
        function CleanOrgan() {
            setValue('txtOrganName', '');
            setValue('txtOrganLabel', '');
            setValue('txtOrderID', '');
            setValue('txtRemark', '');
            Ext.get("OrganStatus").dom.checked = true;
        }
        // 验证正整数
        function limitNum(obj) {
            obj.value = obj.value.replace(/\D/g, '');
            if (obj.value < 0) {
                Ext.get("txtOrderID").dom.value = 0;
            }
            else Ext.get("txtOrderID").dom.value = obj.value;
        }

        // 打开模式窗体的方法。
        function OpenWindow(url, target, w, h, mode, refurbish) {
            var hWin = true;
            if (mode == 'modal') {
                hWin = window.showModalDialog(url, target, 'status:no;scrollbars;no;help:no;resizable:no;menubar=no;toolbar=no;location=no;dialogwidth:' + w + 'px;dialogheight:' + h + 'px');
            }
            else {
                hWin = window.open(url, target, 'width=' + w + 'px,height=' + h +
                                    'px,left=' + (screen.width - w) / 2 + 'px,top=' + (screen.height - h) / 2 +
                                    'px,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,status=no,alwaysRaised=yes');
            }
            if (refurbish) this.location = this.location;  // 刷新父窗体
        }

        // 加载部门树
        function loadTree() {
            sectionTree.root.reload();
            sectionTree.expandPath('/OrganizationTree/1', false);
        }

        // 禁用用户
        function SetStopStatus() {
            SetStatus(0);
        }
        // 启用用户
        function SetStartStatus() {
            SetStatus(1);
        }
        // 删除用户 #开始 ---------------------------------------------------------------------------------------------------------------------------
        function DelUser() {
            var strID = getSelectID(gridUser, 'UserID');
            if (strID == '' || strID == null) {
                Ext.MessageBox.alert("提示", "请选择数据！"); return;
            }
            Ext.MessageBox.confirm("询问", '确认要删除选中的用户吗？删除后将不可恢复！', function(btn) {
                if (btn == "yes") {
                    var strUrl = strPath + 'GGFW/BaseInfo/FoothillUserCmd.fwf?Method=DeleteFoothillUser';
                    Ext.Ajax.request({
                        url: strUrl,
                        method: 'POST',
                        params: {
                            UserIDs: strID
                        },
                        failure: function(response, options) {
                            ReturnValue = Ext.MessageBox.alert("警告", "网络异常或登录过期，请重新登录！");
                        },
                        success: function(response, options) {
                            var json = response.responseText;
                            var obj = parseJsonResult(json);

                            if (!obj.Success) {
                                showMessage("错误！", "删除用户出现错误！请查看详细原因。", obj.Message);
                            }
                            else {
                                doSearchUser(false);
                            }
                        }
                    });
                }
            });
        }
        // 设置用户状态 #开始 ---------------------------------------------------------------------------------------------------------------------------
        function SetStatus(Status) {
            var strID = getSelectID(gridUser, 'UserID');
            if (strID == '' || strID == null) {
                Ext.MessageBox.alert("提示", "请选择数据！"); return;
            }
            Ext.MessageBox.confirm("询问", '确认要设置选中用户的状态吗？', function(btn) {
                if (btn == "yes") {
                    var strUrl = strPath + 'GGFW/BaseInfo/FoothillUserCmd.fwf?Method=SetFoothillUserStatus&UserIDs=' + strID;
                    Ext.Ajax.request({
                        url: strUrl,
                        method: 'POST',
                        params: {
                            Status: Status
                        },
                        failure: function(response, options) {
                            ReturnValue = Ext.MessageBox.alert("警告", "网络异常或登录过期，请重新登录！");
                        },
                        success: function(response, options) {
                            var json = response.responseText;
                            var obj = parseJsonResult(json);

                            if (!obj.Success) {
                                showMessage("错误！", "设置用户状态出现错误！请查看详细原因。", obj.Message);
                            }
                            else {
                                doSearchUser(false);
                            }
                        }
                    });
                }
            });
        }

        // 设置用户密码
        function ShowSetUserPassWin() {
            var row = gridUser.getSelections();
            if (row.length > 1) {
                Ext.MessageBox.alert("提示", "只能单个修改用户的密码！"); return;
            }
            var strID = getSelectID(gridUser, 'UserID');
            if (strID == '' || strID == null) {
                Ext.MessageBox.alert("提示", "请选择数据！"); return;
            }
            OpenWindow('UserDetail.aspx?UserID=' + strID + '&isEdit=' + true + '&isPassWord=' + true, '_top', 620, 170, 'modal', false);
        }

        // 编辑用户信息
        function GetUserInfo(UserID) {
            OpenWindow('UserDetail.aspx?UserID=' + UserID + '&isEdit=' + true, '_top', 620, 290, 'modal', false);
        }

        // 显示用户窗体
        function ShowUserWin() {
            UserID = '';

            OpenWindow('UserDetail.aspx?UserID=' + UserID + '&isEdit=' + false + '&OrganID=' + OrganID + '&OrganName=' + OrganName, '_top', 620, 320, 'modal', false);
        }


        // 重新加载用户
        function doSearchUser(isFirstPage) {
            var UserName = Ext.get("txtUserName").dom.value;
            var TrueName = Ext.get("txtTrueName").dom.value;
            var Status = Ext.get("chStatus").dom.checked;
            var Forbid = Ext.get("chStatusNo").dom.checked;
            if (Status && Forbid) Status = 2;
            else if (Status && !Forbid) Status = 1;
            else if (!Status && Forbid) Status = 0;
            else Status = "3";
            if (storeUser == null) return;
            var startNo = 0; if (!isFirstPage) startNo = (gridUser.getBottomToolbar().getPageData().activePage - 1) * pageSize;
            storeUser.load({ params: { start: startNo, limit: pageSize, UserName: UserName, TrueName: TrueName, Status: Status, OrganID: OrganID} });
        }

        // 打开模式窗体的方法。
        function OpenWindow(url, target, w, h, mode, refurbish) {
            var hWin = true;
            if (mode == 'modal') {
                hWin = window.showModalDialog(url, target, 'status:no;scrollbars;no;help:no;resizable:no;menubar=no;toolbar=no;location=no;dialogwidth:' + w + 'px;dialogheight:' + h + 'px');
                doSearchUser(false);
            }
            else {
                hWin = window.open(url, target, 'width=' + w + 'px,height=' + h +
                                        'px,left=' + (screen.width - w) / 2 + 'px,top=' + (screen.height - h) / 2 +
                                        'px,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,status=no,alwaysRaised=yes');
            }
            if (refurbish) this.location = this.location;  // 刷新父窗体
        }